package reducejoin;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class TableReducer extends Reducer<Text, TableBean, TableBean, NullWritable> {
    @Override
    protected void reduce(Text key,
                          Iterable<TableBean> values,
                          Context context) throws IOException, InterruptedException {
        List<TableBean> orderBeans = new ArrayList<>();
        TableBean pdBean = new TableBean();

        for (TableBean bean : values) {
            //0是订单表 --pid
            if ("0".equals(bean.getFlag())) {
                TableBean orderBean = new TableBean();
                try {
                    //bean copy To orderBean
                    BeanUtils.copyProperties(orderBean,bean);
                } catch (Exception e) {
                    e.printStackTrace();
                }

                orderBeans.add(orderBean);
            }else {
                //1是产品表--pid
                try {
                    BeanUtils.copyProperties(pdBean,bean);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        for (TableBean bean : orderBeans) {
            bean.setPname(pdBean.getPname());
            context.write(bean,NullWritable.get());
        }


    }
}
